package cj.web.portal.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.admin.constants.CategoryEnum;
import cj.web.admin.constants.ResourceCategoryEnum;
import cj.web.admin.constants.SourceEnum;
import cj.web.portal.dao.ConfigDAO;
import cj.web.portal.dao.HomeDAO;
import cj.web.portal.dao.LineDAO;
import cj.web.portal.dao.ResDAO;
import cj.web.portal.utils.ConfigConstants;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

/**
 * 首页相关
 * @author HanMin
 *
 * 2015年12月29日
 */
@Path("/")
public class HomeController {

	private static Logger logger = Logger.getLogger(HomeController.class.getName());
	
	@Autowired
	private HomeDAO homeDAO;
	
	@Autowired
	private ConfigDAO configDAO;
	
	@Autowired
	private LineDAO lineDAO;
	
	@Autowired
	private ResDAO resDAO;
	
	@Get("85888888")
	public String red85888888()
	{
		return "r:qb/";
	}

	@Get("")
	public String home(Model model)
	{
		//首页banner
		List<Map> banners = configDAO.queryByCode(ConfigConstants.HOME_BANNER);
		
		//首页LB
		List<Map> pics = configDAO.queryByCode(ConfigConstants.HOME_LB);
		
		model.add("pics", pics);
		model.add("banner", banners==null?"":banners.get(0).get("value"));
		return "home.vm";
	}
	
	//首页
	@Get("getHome")
	public String getHome(@Param("category") String category,
			@Param("start") int start,@Param("length") int length)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<String> cats = new ArrayList<String>();
		if("".equals(category))
		{
			cats.add("0");
			cats.add("1");
			cats.add("2");
			cats.add("3");
		}
		else
		{
			cats.add(category);
		}
		
		//查询首页长江头条,品牌活动,微聚合列表
		List<Map> reps = homeDAO.queryByInCategory(cats, start*length, length);
		List<Map> homeRepsList = new ArrayList<Map>();
		for(Map rep :reps)
		{
			Map homeRep = new HashMap();
			homeRep.put("id", rep.get("id"));//ID
			homeRep.put("title", rep.get("title"));//标题
			homeRep.put("categoryClass", rep.get("category"));//类别Id
			homeRep.put("source",SourceEnum.getDefaultImg(Integer.parseInt(rep.get("source").toString())));//来源
			homeRep.put("abstractInfor", rep.get("abstractInfor"));//简介
			
			//缩率图
			String carouselImage = rep.get("carouselImage")==null?"":rep.get("carouselImage").toString();
			if(carouselImage !=null && !"".equals(carouselImage))
			{
				String carouselImages[] = carouselImage.split("/");
				homeRep.put("carouselImage", carouselImages[0]+"/s_"+carouselImages[1]);//缩率图
			}
			else
			{
				homeRep.put("carouselImage", "");//缩率图
			}
			
			homeRep.put("releaseAT", DateUtils.getDateTime2Str(
					DateUtils.parse(rep.get("releaseAT").toString()), DateUtils.DATE_FULL_STR));//发布时间
			
			homeRepsList.add(homeRep);
		}
		
		jo.put("list", homeRepsList);
		
		return "@"+jo.toString();
	}
	
	//集团简介
	@Get("jtjj")
	public String getHomeJJ(Model model)
	{
		model.add("head", "home");
		model.add("memu","jtjj");
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		model.add("jtpcjtts", jtpphd(String.valueOf(CategoryEnum.CJTT_CJTT.getValue())));//集团长江头条列表
		return "jtjj.vm";
	}
	
	//主要领导
	@Get("lead")
	public String getHomeLRLD(Model model)
	{
		model.add("head", "home");
		model.add("memu","lead");
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		return "lrld.vm";
	}
	
	//组织机构
	@Get("zzjg")
	public String getHomeZZJG(Model model)
	{
		model.add("head", "home");
		model.add("memu","zzjg");
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		return "zzjg.vm";
	}
	
	//集团产业
	@Get("jtcy")
	public String getHomeJTCY(Model model)
	{
		model.add("head", "home");
		model.add("memu","jtcy");
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		return "JTCY.vm";
	}
	
	//大事记
	@Get("dsj")
	public String getHomeDSJ(Model model)
	{
		//查询结果按照标题倒序、日期倒序
		List<Map> reps = resDAO.queryByCategory(String.valueOf(ResourceCategoryEnum.DSJSJZ.getValue()));
		model.add("data",reps);
		model.add("memu","dsj");
		model.add("head", "home");
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		return "dsj.vm";
	}
	
	/**
	 * 集团down
	 * @param category
	 * @return
	 */
	private List<Map> jtpphd(String category)
	{
		//集团品牌活动
		List<Map> ppls = lineDAO.query(category, 0, 5);
		List<Map> pphdls = new ArrayList<Map>();
		for(Map rep :ppls)
		{
			Map l_rep = new HashMap();
			l_rep.put("id", rep.get("id"));//ID
			String title=rep.get("title").toString();
			if(title.length()>15)
			{
				title=title.toString().substring(0, 15)+"...";
			}
			l_rep.put("title", title);//标题
			l_rep.put("abstractInfor", rep.get("abstractInfor"));//简介
			
			//缩率图
			String carouselImage = rep.get("carouselImage")==null?"":rep.get("carouselImage").toString();
//			if(carouselImage !=null && !"".equals(carouselImage))
//			{
//				String carouselImages[] = carouselImage.split("/");
//				l_rep.put("carouselImage", carouselImages[0]+"/s_"+carouselImages[1]);//缩率图
//			}
//			else
//			{
//				l_rep.put("carouselImage", "");//缩率图
//			}
			l_rep.put("carouselImage", carouselImage);//缩率图
			pphdls.add(l_rep);
		}
		return pphdls;
	}
}
